﻿using System;
using System.Web.Services;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace BieuDoGiaVang.Services
{
    /// <summary>
    /// Summary description for UpdateGiaVang
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class UpdateGiaVang : System.Web.Services.WebService
    {
        [WebMethod]
        public string UpdateGiaVangSJC(string giaMua,string giaBan,string ngay)
        {
            int result = 0;
            string strDate = "";
            string sqlConn = ConfigurationManager.ConnectionStrings["GiaVangConnectionString"].ConnectionString;
            string queryUpdate = "update VangSJC set GiaMua=@GiaMua, GiaBan=@GiaBan, NgayCapNhat=GETDATE() " +
                            "where Ngay=@Ngay AND (GiaMua!=@GiaMua OR GiaBan!=@GiaBan)";
            string queryInsert = "INSERT INTO VangSJC (Ngay,GiaMua,GiaBan) Values(@Ngay,@GiaMua,@GiaBan)";
            string querySelectDate = "select giamua,giaban,ngaytao,ngaycapnhat from VangSJC where ngay='" + ngay + "'";
            using(SqlConnection conn = new SqlConnection())
            {                
                conn.ConnectionString = sqlConn;
                conn.Open();
                string strGiaMua = ConvertToStrNumber(giaMua);
                string strGiaBan = ConvertToStrNumber(giaBan);
                 DataSet dsData = new DataSet();
                using (SqlDataAdapter ad = new SqlDataAdapter(querySelectDate, conn))
                {
                    ad.Fill(dsData);
                }
                //Da co du lieu => Update
                if (dsData != null && dsData.Tables[0].Rows.Count > 0)
                {
                    if (strGiaMua.Equals(dsData.Tables[0].Rows[0]["GiaMua"].ToString()) &&
                        strGiaBan.Equals(dsData.Tables[0].Rows[0]["GiaBan"].ToString()))
                    {
                        strDate = dsData.Tables[0].Rows[0]["NgayCapNhat"].ToString();
                        if (dsData.Tables[0].Rows[0]["NgayCapNhat"].ToString().Equals(string.Empty))
                        {
                            strDate = dsData.Tables[0].Rows[0]["NgayTao"].ToString();
                        }
                    }
                    else
                    {
                        using (SqlCommand cmd = new SqlCommand(queryUpdate, conn))
                        {
                            cmd.Parameters.Add("@GiaMua", strGiaMua);
                            cmd.Parameters.Add("@GiaBan", strGiaBan);
                            cmd.Parameters.Add("@Ngay", ngay);
                            result = cmd.ExecuteNonQuery();
                        }                        
                    }
                }
                else//Chua co du lieu -> Insert
                {
                    using (SqlCommand cmd = new SqlCommand(queryInsert, conn))
                    {
                        cmd.Parameters.Add("@GiaMua", strGiaMua);
                        cmd.Parameters.Add("@GiaBan", strGiaBan);
                        cmd.Parameters.Add("@Ngay", ngay);
                        result = cmd.ExecuteNonQuery();
                    }
                }
                conn.Close();
            }

            //Tra Ket Qua Ve
            if (result > 0)
            {
                return DateTime.Now.ToString();
            }
            return strDate;
        }

        /// Convert chuỗi số có dấu tiền tệ(46,000) thành chuỗi số(46000)
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        private string ConvertToStrNumber(string num)
        {
            string strResult = "";
            foreach (char item in num)
            {
                if (char.IsDigit(item))
                {
                    strResult += item.ToString(); 
                }
            }
            return strResult;
        }
    }
}
